package com.managertrade.service.impl;

import com.managertrade.dao.sqlite.entity.StrategyTrade;
import com.managertrade.dao.sqlite.repository.StrategyTradeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class StrategyTradeService {

    @Autowired
    StrategyTradeRepository strategyTradeRepository;

    // 保存交易记录
    public StrategyTrade save(StrategyTrade trade) {
        return strategyTradeRepository.save(trade);
    }

    // 根据策略ID获取交易记录
    public List<StrategyTrade> findByStrategyId(Long strategyId) {
        List<StrategyTrade> all = strategyTradeRepository.findAll();
        if(!CollectionUtils.isEmpty(all)){
            all = all.stream().filter(k->k.getStrategyId().equals(strategyId)).collect(Collectors.toList());
        }
        return all;
    }
    public List<StrategyTrade> findByStrategyIdAndProfitAmountIsNull(Long strategyId) {
        List<StrategyTrade> all = strategyTradeRepository.findByStrategyIdAndProfitAmountIsNull( strategyId);
        return all;
    }
    // 根据ID获取交易记录
    public Optional<StrategyTrade> findById(Long id) {
        return strategyTradeRepository.findById(id);
    }

    // 删除交易记录
    public void deleteById(Long id) {
        strategyTradeRepository.deleteById(id);
    }
}
